using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using KinectTD.Input;
using KinectTD.State_Management;
using KinectTD.Game_Management;
using KinectTD.UI;
namespace KinectTD
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class KinectGame : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;


        private MenuScreen mainMenu;
        private Color backColor = new Color(74, 132, 199);
        private Texture2D backTexture;

        public KinectGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Window.Title = "Kinect TD";
            graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;

            graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            this.IsMouseVisible = true;
            Kinect.Init();
            base.Initialize();

            Engine.GameStack = new ScreenStack(this);

            Engine.gamePlayer = new Player();

            List<Button> menuButtons = new List<Button>();

            Button playButton = new Button("Buttons/play");
            playButton.Size = new Vector2(400, 157);
            playButton.Position = new Vector2(GraphicsDevice.Viewport.Width / 2 - (playButton.Size.X / 2), GraphicsDevice.Viewport.Height / 3);
            playButton.Clicked += new Button.ClickedEventHandler(playButton_Clicked);

            menuButtons.Add(playButton);


            Button quitButton = new Button("Buttons/quit");
            quitButton.Size = playButton.Size;
            quitButton.Position = new Vector2(playButton.Position.X, playButton.Position.Y + quitButton.Size.Y + 50);
            quitButton.Clicked += new Button.ClickedEventHandler(quitButton_Clicked);

            menuButtons.Add(quitButton);

            mainMenu = new MenuScreen(menuButtons, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            mainMenu.BackgroundImage = backTexture;

            Engine.MainMenu = mainMenu;
            Engine.GameStack.Push(mainMenu);

        }

        void quitButton_Clicked(object sender, EventArgs a)
        {
            this.Exit();
        }

        void playButton_Clicked(object sender, EventArgs a)
        {
            Engine.GameStack.Push(new GameScreen(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
        }



        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            backTexture = Content.Load<Texture2D>("Menu");
            Engine.UtilFont = Content.Load<SpriteFont>("utilFont");
            Engine.TestTexture = Content.Load<Texture2D>("textureTest");

            Kinect.Cursor = Content.Load<Texture2D>("cursor");


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {

          Kinect.Unload();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {


            Window.Title = "Kinect:TD // Running slowly: " + gameTime.IsRunningSlowly;
            Engine.GameViewport = GraphicsDevice.Viewport;
            Kinect.Update();
            KeyboardHelper.Update();
            MouseHelper.Update();

            Engine.GameStack.Update(gameTime);



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(backColor);


            Engine.GameStack.Draw();
           Kinect.Draw(spriteBatch);


            base.Draw(gameTime);
        }
    }
}
